import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;

import org.joda.time.DateTime;
import org.junit.Test;

//@author: Zhang Mengdi A0105699R
public class CommandTest {
	
	private static final DateTime FLOATING_DEADLINE  = new DateTime(9999,12,31,23,59,0,0);
	private static final DateTime MIN_DATETIME = new DateTime(0);
    private static final DateTime MAX_DATETIME = FLOATING_DEADLINE;
	private static final DateTime[] floating = {MIN_DATETIME, MAX_DATETIME};
    private static final Vector<DateTime> FLOATING = new Vector<DateTime>(Arrays.asList(floating));
	@Test
	public void testFloatingTask() {
		String cmd = "add go to gym";
		Command command = new Command(cmd);
		String actualDes = command.getDescription();
		String expectedDes = "go to gym";
		
		Vector<DateTime> actualDateTime = command.range;
		Vector<DateTime> expectedDateTime = FLOATING;
		
		assertEquals(actualDes, expectedDes);
		assertEquals(actualDateTime, expectedDateTime);				
	}
	@Test
	public void testDeadlinedTask(){
		String cmd = "add finish homework by fri 5pm";
		Command command = new Command(cmd);
		String actualDes = command.getDescription();
		String expectedDes = "finish homework";
		
		Vector<DateTime> actualDateTime = command.range;
		DateTime expectedDeadline = new DateTime(2014, 4, 18, 17, 0, 0, 0);
		DateTime[] deadlines = {expectedDeadline, expectedDeadline};
		Vector<DateTime> expectedDateTime = new Vector<DateTime>(Arrays.asList(deadlines));
		
		
		assertEquals(actualDes, expectedDes);
		assertEquals(actualDateTime, expectedDateTime);
		
	}
	@Test
	public void testTimedTask(){
		String cmd = "add do homework from today 5pm to 10pm ";
		Command command = new Command(cmd);
		String actualDes = command.getDescription();
		String expectedDes = "do homework";
		
		Vector<DateTime> actualDateTime = command.range;
		DateTime expectedStart = new DateTime(2014, 4, 14, 17, 0, 0, 0);
		DateTime expectedEnd = new DateTime(2014, 4, 14, 22, 0, 0, 0);
		DateTime[] startEnd = {expectedStart, expectedEnd};
		Vector<DateTime> expectedDateTime = new Vector<DateTime>(Arrays.asList(startEnd));
		
		
		assertEquals(actualDes, expectedDes);
		assertEquals(actualDateTime, expectedDateTime);
	}
	BufferedWriter writer;
	BufferedReader reader;
	private static File createFile(String fileName) {
		File file = new File(fileName); 
		if(!file.exists()) { 
			  try {  
			    file.createNewFile(); 
			  } 
			  catch(IOException ioe) { 
			    ioe.printStackTrace(); 
			  } 
		}
		return file;
	}
	
	@Test
	public void testDescriptionInCases() throws Exception{
		String commandFileName = "commands";
		File CommandFile = createFile(commandFileName);
		writer = new BufferedWriter(new FileWriter(CommandFile));
		writer.write("add go by bus /by next next fri"); writer.newLine();
		writer.write("add go by bus /by 4pm"); writer.newLine();
		writer.write("add go by bus by myself");writer.newLine();
		writer.write("add go by bus in 5 days"); writer.newLine();
		writer.write("add go by next bus");writer.newLine();
		writer.write("add go by next tue");writer.newLine();
		writer.write("add go by bus in school in Singapore");writer.newLine();
		writer.write("add go by bus from 20/4 to 28/4");writer.newLine();
		writer.write("add go by bus from 28/4 1pm to 6pm");writer.newLine();
		writer.write("add go by bus from Science to Soc");writer.newLine();
		writer.write("edit 3 go in the classroom /in 2 days");writer.newLine();
		writer.write("edit 4 go in the classromm by 28/4 5pm");writer.newLine();
		writer.write("edit 2 go in the classroom by myself");writer.newLine();
		writer.close();
		
		reader = new BufferedReader(new FileReader(CommandFile));
		String cmd = reader.readLine();
		String descriptionFileName = "descriptions";
		File descriptionFile = createFile(descriptionFileName);
		BufferedWriter descriptionWriter = new BufferedWriter(new FileWriter(descriptionFile));
		while(cmd!=null){
			Command command = new Command(cmd);
			descriptionWriter.write(command.getDescription());
			descriptionWriter.newLine();
			cmd = reader.readLine();			
		}
		descriptionWriter.close();
		
		String fileName = "expectedDescription";
		File expectedDescription = createFile(fileName);
		BufferedReader descriptionReader = new BufferedReader(new FileReader(descriptionFile));
		BufferedReader expectedDescriptionReader = new BufferedReader(new FileReader(expectedDescription));
		String expectedDes = expectedDescriptionReader.readLine();
		String actualDes = descriptionReader.readLine();
		while(expectedDes != null){
			expectedDes = expectedDescriptionReader.readLine();
			actualDes = descriptionReader.readLine();
			assertEquals(expectedDes, actualDes);
		}
		descriptionReader.close();
		expectedDescriptionReader.close();
		
		
	}
	

}
